package com.zjy.weather.base

import android.graphics.Color
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v4.app.FragmentActivity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.zjy.weather.util.statusbar.SystemBarTintManager

/**
 * Created by zhongjy on 2017/11/3.
 * Copyright (C) 2017 zhongjy. All rights reserved.
 */
open class BaseActivity : FragmentActivity() {

    lateinit var mSystemBarTintManager: SystemBarTintManager

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }

    protected fun enableTranslucentStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN) //去掉信息栏
            window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) //显示状态栏

            val windowParams = window.attributes
            windowParams.flags = windowParams.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            window.attributes = windowParams

            // create our manager instance after the content view is set
            mSystemBarTintManager = SystemBarTintManager(this)
            mSystemBarTintManager.setStatusBarTintEnabled(true)
            mSystemBarTintManager.setStatusBarTintColor(Color.TRANSPARENT)
        }
    }

    //状态栏显示隐藏设置
    fun setClipPaddingView(clipPaddingView: View?) {
        if (clipPaddingView == null) {
            return
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val statusBarHeight = getStatusBarHeight()
            if (statusBarHeight > 0) {
                clipPaddingView.setPadding(clipPaddingView.paddingLeft, statusBarHeight,
                        clipPaddingView.paddingBottom, clipPaddingView.paddingBottom)
                if (clipPaddingView.layoutParams.height != ViewGroup.LayoutParams.WRAP_CONTENT
                        && clipPaddingView.layoutParams.height != ViewGroup.LayoutParams.MATCH_PARENT) {
                    clipPaddingView.layoutParams.height = clipPaddingView.layoutParams.height + statusBarHeight
                }
            }
        }
    }

    fun getStatusBarHeight(): Int {
        var statusHeight = 0
        val localRect = Rect()
        window.decorView.getWindowVisibleDisplayFrame(localRect)
        statusHeight = localRect.top
        if (0 == statusHeight) {
            val localClass: Class<*>
            try {
                localClass = Class.forName("com.android.internal.R\$dimen")
                val localObject = localClass.newInstance()
                val i5 = localClass.getField("status_bar_height").get(localObject).toString().toInt()
                statusHeight = resources.getDimensionPixelSize(i5)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
        return statusHeight
    }
}